<?php
/**
 * 外部接口类
 */
ini_set('display_errors', 'on');
error_reporting( E_ALL );

//Terry ref: http://blog.csdn.net/blueling51/article/details/7942812
header("Access-Control-Allow-Origin: *");

//$http_url="http://localhost/myApp/";

$http_url = "http://".$_SERVER["HTTP_HOST"]."/";

$apiname=$_REQUEST["apiname"];
/*
$url=curPageURL();
$url=explode("?", $url);

$param=splitParam($url[1]);
$http=explode("api.php", $url[0]);
*/
$param=$_REQUEST;
switch ($apiname){
	case "login":
		$postUrl=$http_url."?g=Home&m=Api&a=login";
		$rev=curlPost($postUrl,$param);
		echo $rev;
		break;
	case "getorder":
		$postUrl=$http_url."?g=Home&m=Api&a=getorder";
		$rev=curlPost($postUrl,$param);
		echo $rev;
		break;
	case "uploadFile":
		$__shijian=date("Ym",time());
		$up_url="Uploads/AZK/".$__shijian;
		if(is_dir($up_url)){
			
		}else{
			mkdir($up_url,0777);
			
		}
		$uUrl=uploadCardNoOrderId($_FILES,$up_url);
		if($uUrl){
			$arr=array("result"=>true,"file_url"=>$uUrl);
		}else{
			$arr=array("result"=>false,"msg"=>"目录创建失败");
		}
		echo json_encode($arr);
		break;
	case "finish":
		$postUrl=$http_url."?g=Home&m=Api&a=finish";
		$rev=curlPost($postUrl,$param);
		echo $rev;
		break;
	case "finishorder":
		$postUrl=$http_url."?g=Home&m=Api&a=finishorder";
		$uploadUrl=$http_url."?g=Home&m=Api&a=getUploadUrl";
		$o_id=$param["o_id"];
		$upload_url=curlPost($uploadUrl,array("o_id"=>$o_id));
		$upload_url=json_decode($upload_url,true);
		$uUrl=uploadCard($_FILES,$upload_url);
		/*$__pa=array();
		foreach ($param as $key=>$val){
			if($key=="o_card"){
				$__pa[$key]=$uUrl;
			}else{
				$__pa[$key]=$val;
			}
		}
		$__pa[$key]=$uUrl;
		*/
		if($uUrl){
			$param["o_card"]=$uUrl;
		}
		$rev=curlPost($postUrl,$param);
		echo $rev;
		break;
	case "setxy":
		$postUrl=$http_url."?g=Home&m=Api&a=setxy";
		$rev=curlPost($postUrl,$param);
		echo $rev;
		break;
	default:
		$arr=array("result"=>false,"msg"=>"接口参数错误");
		echo json_encode($arr);
}
exit;
function uploadCard($files,$urls){
	$up_url=$urls["url"];
	$o_no=$urls["o_no"];
	$_file=basename($files["o_card"]["name"]);
		
	$fileExt=getFileExt($_file);
	//$fileNotExt=getFileNoExt($__file);
	$__file=$o_no.".".$fileExt;
	$__url=$up_url."/".$__file;
	if(file_exists($__url)){
		$_rand=mt_rand(1, 10);
		$__file=$o_no."-".$_rand.".".$fileExt;
		$__url=$up_url."/".$__file;
	}
	//$__url=dirname(__FILE__)."/".$__url;
	
	if(move_uploaded_file($files["o_card"]["tmp_name"], $__url)){
		return $__url;
	}else{
		return false;
	}
}
function uploadCardNoOrderId($files,$urls){
	$up_url=$urls;
	$_file=basename($files["file"]["name"]);
		
	$fileExt=getFileExt($_file);
	$_rand=mt_rand(1, 10);
	$file_name=date("YmdHis")."-".$_rand.".".$fileExt;
	$__url=$up_url."/".$file_name;
	
	if(move_uploaded_file($files["file"]["tmp_name"], $__url)){
		return $__url;
	}else{
		return false;
	}
}
function getFileExt($file){
	return substr(strrchr($file, '.'), 1);
}
function curPageURL() 
{
    $pageURL = 'http';

    if (@$_SERVER["HTTPS"] == "on") 
    {
        $pageURL .= "s";
    }
    $pageURL .= "://";

    if ($_SERVER["SERVER_PORT"] != "80") 
    {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } 
    else 
    {
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

function curlPost( $url, $params ){

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HTTP_VERSION, 1.0);
		curl_setopt($ch, CURLOPT_POST, true);
		if( isArrayParam( $params ) ){	
			curl_setopt($ch, CURLOPT_POSTFIELDS, data_encode($params));
		}else{
			curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
		}
		ob_start();
		curl_exec($ch);
		$data=ob_get_contents();
		ob_end_clean();
		curl_close ($ch);
		return $data;		
}
function data_encode($data, $keyprefix = "", $keypostfix = "") {
	//assert( is_array($data) );	//Server has disabled assert..
	$vars=null;
	foreach($data as $key=>$value) {
		if(is_array($value)){
			$vars .= data_encode($value, $keyprefix.$key.$keypostfix.("["), ("]"));
		}else{
			$vars .= $keyprefix.$key.$keypostfix."=".urlencode($value)."&";
		}
	}
	return $vars;
}
function isArrayParam($param){
	if( is_array($param) ){
		foreach ($param as $key => $val) {
			if( !is_array( $val ) ){
				if( substr( $val, 0, 1 ) == '@' ){
					//如果是file域，则不认为是数组
					return false;
				}					
			}
		}
		return true;
	}else{
		return false;
	}
}

function splitParam($str){
	$out=array();
	$list=explode("&", $str);
	if(is_array($list) && count($list)){
		foreach ($list as $val){
			$tmp=explode("=", $val);
			$out[$tmp[0]]=$tmp[1];
		}
	}
	return $out;
}